package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author huangy
 * @description
 * @since 2022/11/10 14:02
 **/
@Component
@Slf4j
public class RocketMqQueueSelector implements MessageQueueSelector {
    @Override
    public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
        list.forEach(messageQueue -> {
            log.info("topic: " + messageQueue.getTopic());
            log.info("brokerName: " + messageQueue.getBrokerName());
            log.info("queueId: " + messageQueue.getQueueId());
        });
        log.info("message" + message.toString());
        Integer id = (Integer) o;
        int index = id % list.size();
        log.info("id=" + id + ", index=" + index + ", mqs.size=" + list.size());
        return list.get(index);
    }
}
